﻿namespace LiteBlog
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Profile;
    using System.Web.Routing;
    using System.Web.Security;
    using LiteBlog.Common;
    using LiteBlog.Models;
    using LiteBlog.Components;
    using LiteBlog.Common.Entities;
    using LiteBlog.Components.Contracts;
    using LiteBlog.Infrastructure;


    public class MvcApplication : System.Web.HttpApplication
    {
        private static IStatComp _statComp;

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var isSite = Application["IsSite"];
            if (isSite == null)
            {
                var path = HttpContext.Current.Request.ApplicationPath;
                var site = HttpContext.Current.Request.Url.Authority;
                isSite = (path == "/") ? true : false;
                Application["IsSite"] = isSite;
                Application["Site"] = site;
            }
        }

        protected void Application_End(object sender, EventArgs e)
        {
            var stat = (Stat)Application["Stat"];
            _statComp.Save(stat.Clone());
        }

        protected void Application_Start()
        {
            _statComp = StartupHelper.Startup();
            _statComp.Load();
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            _statComp.IncrementVisits();
        }
    }
}